import { useMessage } from "naive-ui";
export default function useSocket(url, handleMessage) {
  const ws = new WebSocket(url);
  const message = useMessage();
  // 监听ws事件
  ws.addEventListener("open", handleOpen);
  ws.addEventListener("error", handleError);
  ws.addEventListener("close", handleClose);
  ws.addEventListener("message", handleMessage);

  async function handleOpen() {
    message.info("指纹仪连接成功！可以进行指纹识别！！");
  }

  function handleError() {
    message.error("指纹仪发生错误");
  }

  function handleClose() {
    message.error("指纹仪关闭");
  }

  function sendMsg(msg) {
    ws.send(msg);
  }

  return {
    ws,
    sendMsg,
  };
}
